package com.ruoyi.project.system.dangjianzixun.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.cms.domain.CommonCms;
import com.ruoyi.project.system.cms.service.ICommonCmsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 政策文件Controller
 * 
 * @author ruoyi
 * @date 2023-07-15
 */
@Controller
@RequestMapping("/hui/manage/zcwj")
public class ZcwjController extends BaseController
{
    private String prefix = "hui/manage/zcwj";
    private final String MODULETYPE = "Z2";

    @Autowired
    private ICommonCmsService commonCmsService;

    @RequiresPermissions("hui:manage:zcwj:view")
    @GetMapping()
    public String zcwj()
    {
        return prefix + "/zcwj";
    }

    /**
     * 查询政策文件列表
     */
    @RequiresPermissions("hui:manage:zcwj:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CommonCms commonCms)
    {
        commonCms.setModuleType(MODULETYPE);
        startPage();
        List<CommonCms> list = commonCmsService.selectCommonCmsList(commonCms);
        return getDataTable(list);
    }

    /**
     * 导出政策文件列表
     */
    @RequiresPermissions("hui:manage:zcwj:export")
    @Log(title = "政策文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CommonCms commonCms)
    {
        commonCms.setModuleType(MODULETYPE);
        List<CommonCms> list = commonCmsService.selectCommonCmsList(commonCms);
        ExcelUtil<CommonCms> util = new ExcelUtil<CommonCms>(CommonCms.class);
        return util.exportExcel(list, "政策文件数据");
    }

    /**
     * 新增政策文件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存政策文件
     */
    @RequiresPermissions("hui:manage:zcwj:add")
    @Log(title = "政策文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CommonCms commonCms)
    {
        commonCms.setModuleType(MODULETYPE);
        return toAjax(commonCmsService.insertCommonCms(commonCms));
    }

    /**
     * 修改政策文件
     */
    @RequiresPermissions("hui:manage:zcwj:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CommonCms commonCms = commonCmsService.selectCommonCmsById(id);
        mmap.put("commonCms", commonCms);
        return prefix + "/edit";
    }

    /**
     * 修改保存政策文件
     */
    @RequiresPermissions("hui:manage:zcwj:edit")
    @Log(title = "政策文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CommonCms commonCms)
    {

        return toAjax(commonCmsService.updateCommonCms(commonCms));
    }

    /**
     * 删除政策文件
     */
    @RequiresPermissions("hui:manage:zcwj:remove")
    @Log(title = "政策文件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(commonCmsService.deleteCommonCmsByIds(Convert.toLongArray(ids)));
    }
    
    /**
     * 查看政策文件内容
     */
    @GetMapping("/view/{id}")
    @Log(title = "政策文件", businessType = BusinessType.OTHER)
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        CommonCms commonCms = commonCmsService.selectCommonCmsById(id);
        mmap.put("commonCms", commonCms);
        return prefix + "/view";
    }
    
    /**
     * 上传文件（图片或支付文件）
     */
    @RequiresPermissions("hui:manage:zcwj:upload")
    @Log(title = "政策文件", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file)
    {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String fileName = FileUploadUtils.upload(filePath, file,MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, true);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}